package com.gcsoftware.gateway.filters.http.response;

import com.gcsoftware.gateway.GWImpl.GwAccessControl;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpVersion;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by qs on 2018/4/17.
 */
public class RspValidations {
   /* public FullHttpResponse httpRspValidations(Map<String, Object> argsMap, FullHttpResponse resp) {
        if (null != argsMap && argsMap.size() > 0 && null != argsMap.get("reqHasContent")) {
            if (null == argsMap.get("token") || StringUtils.isBlank((String) argsMap.get("token"))) {
                resp.setStatus(GwHttpRspStatus.TOKEN_REQUIRED);
                return resp;
            }

            if (null == argsMap.get("roleId") || StringUtils.isBlank((String)argsMap.get("roleId"))) {
                resp.setStatus(GwHttpRspStatus.UNAUTHORIZED);
                return resp;
            }

            if (null == argsMap.get("appId") || StringUtils.isBlank((String)argsMap.get("appId"))) {
                resp.setStatus(GwHttpRspStatus.UNAUTHORIZED_TOKEN);
                return resp;
            }

            if (null == argsMap.get("appIps") || ((List)argsMap.get("appIps")).size() == 0) {
                return resp.setStatus(GwHttpRspStatus.APP_IP_REQUIRED);
            } else if (!((List)argsMap.get("appIps")).contains(argsMap.get("sourceIp"))) {
                return resp.setStatus(GwHttpRspStatus.IP_REJECTED);
            }

            if (null == GwAccessControl.getGwAccessUriMap((String)argsMap.get("roleId"),(String)argsMap.get("endPoint"),(String)argsMap.get("targetMethod"))) {
                return resp.setStatus(GwHttpRspStatus.FORBIDDEN);
            }

            //如果使用统一入口的接口方法，则必须要定义接口标识字段，且服务号名称不能为空
            String interfServ = GwAccessControl.getUnifyInterfServ((String)argsMap.get("roleId"),(String)argsMap.get("endPoint"));
            if (StringUtils.isNotBlank(interfServ) && StringUtils.isBlank((String)argsMap.get("serviceId"))) {
                return resp.setStatus(GwHttpRspStatus.FORBIDDEN);
            }
        }

        return null;
    }*/

    public FullHttpResponse httpRspValidations(Map<String, Object> argsMap) {
        if (null != argsMap && argsMap.size() > 0 && null != argsMap.get("reqHasContent")) {
            if (null == argsMap.get("token") || StringUtils.isBlank((String) argsMap.get("token"))) {
                return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,GwHttpRspStatus.TOKEN_REQUIRED);

            }

            if (null == argsMap.get("roleId") || StringUtils.isBlank((String)argsMap.get("roleId"))) {
                return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,GwHttpRspStatus.UNAUTHORIZED);
            }

            if (null == argsMap.get("appId") || StringUtils.isBlank((String)argsMap.get("appId"))) {
                return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,GwHttpRspStatus.UNAUTHORIZED_TOKEN);
            }

            if (null == argsMap.get("appIps") || ((List)argsMap.get("appIps")).size() == 0) {
                return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,GwHttpRspStatus.APP_IP_REQUIRED);
            } else if (!((List)argsMap.get("appIps")).contains(argsMap.get("sourceIp"))) {
                return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,GwHttpRspStatus.IP_REJECTED);
            }

            if (null == GwAccessControl.getGwAccessUriMap((String)argsMap.get("roleId"),(String)argsMap.get("endPoint"),(String)argsMap.get("targetMethod"))) {
                return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,GwHttpRspStatus.FORBIDDEN);
            }

            //如果使用统一入口的接口方法，则必须要定义接口标识字段，且服务号名称不能为空
            String interfServ = GwAccessControl.getUnifyInterfServ((String)argsMap.get("roleId"),(String)argsMap.get("endPoint"));
            if (StringUtils.isNotBlank(interfServ) && StringUtils.isBlank((String)argsMap.get("serviceId"))) {
                return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,GwHttpRspStatus.FORBIDDEN);
            }
        }

        return null;
    }
}
